package com.ruiysoft.controller.wx;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.ruiysoft.controller.upload.pic.QinuService;
import com.ruiysoft.controller.wx.api.InnerWechatApi;
import com.ruiysoft.controller.wx.api.service.WeChatApiService;
import com.ruiysoft.controller.wx.dto.InnerWeChatACodeDto;
import com.ruiysoft.controller.wx.dto.OrderPayDto;
import com.ruiysoft.controller.wx.res.JsSignResponse;
import com.ruiysoft.entity.sys.SysShopPayPlatform;
import com.ruiysoft.entity.sys.SysShopPersonnel;
import com.ruiysoft.pub.core.Result;
import com.ruiysoft.pub.core.ResultGenerator;
import com.ruiysoft.service.sys.ISysShopPayPlatformService;
import com.ruiysoft.service.sys.ISysShopPersonnelService;
import com.ruiysoft.service.wechat.IWeChatService;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * Created by 化增光 on 2018/12/1.
 */
@RestController
public class InnerWeChatApiController implements InnerWechatApi {

    private static final Logger logger = LoggerFactory.getLogger(InnerWeChatApiController.class);

    private final ObjectMapper objectMapper;

    private final HttpServletRequest request;

    @Resource
    IWeChatService iWeChatService;

    @Resource
    ISysShopPersonnelService iSysShopPersonnelService;

    @Resource
    ISysShopPayPlatformService iSysShopPayPlatformService;

    @Resource
    WeChatApiService weChatApiService;

    @Resource
    QinuService qinuService;

    @org.springframework.beans.factory.annotation.Autowired
    public InnerWeChatApiController(ObjectMapper objectMapper, HttpServletRequest request) {
        this.objectMapper = objectMapper;
        this.request = request;
    }

    @Override
    public Result<JsSignResponse> jssign(@ApiParam(value = "", required = true) @RequestBody @Valid OrderPayDto order) {
        try {
            SysShopPersonnel personnel =  iSysShopPersonnelService.getById(order.getPersonnelId());
            SysShopPayPlatform sysShopPayPlatform = iSysShopPayPlatformService.selectShopPayPlatformByAppId(personnel.getAppId());
            JsSignResponse response = iWeChatService.jsSign(personnel,sysShopPayPlatform,order);
            return ResultGenerator.genSuccessResult(response);
        } catch (Exception e) {
            logger.error("InnerWeChatApiController--jssign:", e);
            return ResultGenerator.genFailResult(e.getMessage());
        }
    }

    @Override
    public Result<String> wxacodeunlimit(HttpServletResponse response, @ApiParam(value = "", required = true) @RequestBody @Valid InnerWeChatACodeDto aCodeDto) {
        try {
            SysShopPayPlatform payPlatform = iSysShopPayPlatformService.selectShopPayPlatformByAppId(aCodeDto.getAppId());
            byte[] img = weChatApiService.wxacodeunlimit(payPlatform.getSecret(),aCodeDto);
            logger.info("wxacodeunlimit: body {}",img.length);
            String res = qinuService.upload(img);
            return ResultGenerator.genSuccessResult(res);
        } catch (Exception e) {
            logger.error("InnerWeChatApiController--wxacodeunlimit:", e);
            return ResultGenerator.genFailResult(e.getMessage());
        }
    }
}
